//
//  SSVedioViewController.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/22.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSVedioViewController.h"
#import "SSHandPickViewCell.h"
#import "SSHandpickModel.h"
@interface SSVedioViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@end

static NSString *const handpickViewCellID = @"handpickViewCellID";
@implementation SSVedioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCollectionView];
    [self requestData];
    
}

- (void) setupCollectionView {
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];

}

- (void) requestData {
    
    [self showHUD];
    [SSNetworkManager getWithURLString:kSSVedioListAPI parameters:nil success:^(NSDictionary *responseDict) {
        for (NSDictionary *dict in responseDict[@"data"]) {
            SSHandpickModel *model = [SSHandpickModel mj_objectWithKeyValues:dict];
            [self.dataSource addObject:model];
        }
        [self.collectionView reloadData];
        [self dismissHUD];
    } failure:^(NSError *error) {
       
        [self dismissHUD];
    }];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SSHandPickViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:handpickViewCellID forIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.row];
    return cell;
    
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.itemSize = CGSizeMake( (kScreenWidth - 40 ) / 2, 120);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
        [_collectionView registerClass:[SSHandPickViewCell class] forCellWithReuseIdentifier:handpickViewCellID];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
